package cn.bingo.yunsj.service;

import cn.bingo.yunsj.common.StateCode;
import cn.bingo.yunsj.common.result.Response;
import cn.bingo.yunsj.controller.model.RpModelAndRule;
import cn.bingo.yunsj.domain.RpModel;
import cn.bingo.yunsj.domain.RpRule;
import cn.bingo.yunsj.mapper.RpModelMapper;
import cn.bingo.yunsj.mapper.RpRuleMapper;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.rule.RuleManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class RuleService {

    @Autowired
    private RpRuleMapper rpRuleMapper;

    @Autowired
    private RpModelMapper rpModelMapper;

    public Response getAll() {
        List<RpRule> rpRules = rpRuleMapper.selectAll();
        /**
         * 这里还需要根据ruleid查询对应得模板
         */
        List<RpModelAndRule> rpModelAndRules = new ArrayList<>();

        rpRules.forEach(e->{
            List<RpModel> rpModels = rpModelMapper.searchByRule(e.getRuleName());
            rpModels.forEach(j->{
                RpModelAndRule rpModelAndRule = new RpModelAndRule();
                BeanUtils.copyProperties(e,rpModelAndRule);
                rpModelAndRule.setType(j.getModelName());
                rpModelAndRules.add(rpModelAndRule);
            });
        });
        return new Response(StateCode.SEARCH_SUCCESS, (long) rpModelAndRules.size(),rpModelAndRules);
    }

    @Transactional
    public Response add(RpModelAndRule rpModelAndRule) {
        // 根据type查看是否有rule to do
        // 判断是不是有这个规则了
        String message = "";
        if(rpRuleMapper.selectByName(rpModelAndRule.getRuleName())!=null){
            message = "规则名已存在，请到修改里面调整相关设置";
        }else{
            RpRule rpRule = new RpRule();
            BeanUtils.copyProperties(rpModelAndRule,rpRule);
            rpRule.setRuleId(UUID.randomUUID().toString());
            rpRule.setUpdateTime(new Date());
            rpRule.setUpdateBy("YUNSJ");
            rpRule.setCreateTime(new Date());
            rpRule.setCreateBy("YUNSJ");
            rpRuleMapper.insert(rpRule);
        }
        // 再执行更新操作,理论上这里还需要传入更新时间，更新人，to do
        rpModelMapper.updateRule(rpModelAndRule.getType(),rpModelAndRule.getRuleName());
        if(message!=""){
            return new Response(StateCode.INSERT_SUCCESS,message);
        }
        return new Response(StateCode.INSERT_SUCCESS);
    }

    public Response update(RpModelAndRule rpModelAndRule) {

        RpRule rpRule = new RpRule();
        BeanUtils.copyProperties(rpModelAndRule,rpRule);
        // 更新rule
        rpRuleMapper.updateByPrimaryKey(rpRule);
        return new Response(StateCode.UPDATE_SUCCESS);
    }

    public Response unbund(RpModelAndRule rpModelAndRule) {
        rpModelMapper.unbund(rpModelAndRule.getType());
        return new Response(StateCode.UNBUND_SUCCESS);
    }
}
